package objectManager;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

import myUtils.vec2;

public abstract class CBaseObject implements IBaseInterface 
{
	// Object member variables.
	private vec2 pos = new vec2 ();
	private vec2 vel = new vec2 ();
	private ByteBuffer vbb;
	private FloatBuffer vertexBuffer;
	private ShortBuffer indexBuffer;
	private FloatBuffer mTextureBuffer;
	private int mTextureID = -1;
	
	private float vertices[] = {
		      -50.0f,  50.0f, 0.0f,  // 0, Top Left
		      -50.0f, -50.0f, 0.0f,  // 1, Bottom Left
		       50.0f, -50.0f, 0.0f,  // 2, Bottom Right
		       50.0f,  50.0f, 0.0f,  // 3, Top Right
		};
	
	private short[] indices = { 0, 1, 2, 0, 2, 3 };
	
	private float textureCoordinates[] = { 0.0f, 0.0f, //
										   0.0f, 1.0f, //
										   1.0f, 1.0f, //
										   1.0f, 0.0f, //
										 };

	// Default Constructor
	public CBaseObject() 
	{
		pos.setX(0.0f);
		pos.setY(0.0f);
		vel.setX(0.0f);
		vel.setY(0.0f);
		
		// set up basic vert data 
		// a float is 4 bytes, therefore we multiply the number of vertices with 4.
		vbb = ByteBuffer.allocateDirect(vertices.length * 4);
		vbb.order(ByteOrder.nativeOrder());
		vertexBuffer = vbb.asFloatBuffer();
		vertexBuffer.put(vertices);
		vertexBuffer.position(0);
		
		// set index data
		ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);
		ibb.order(ByteOrder.nativeOrder());
		indexBuffer = ibb.asShortBuffer();
		indexBuffer.put(indices);
		indexBuffer.position(0);
		
		// set texture data
		ByteBuffer byteBuf = ByteBuffer.allocateDirect(
				textureCoordinates.length * 4);
		byteBuf.order(ByteOrder.nativeOrder());
		mTextureBuffer = byteBuf.asFloatBuffer();
		mTextureBuffer.put(textureCoordinates);
		mTextureBuffer.position(0);
	}

	/*****************************
	 * Class Methods
	 ******************************/
	public void Update(float time) 
	{
	}

	public void Render() 
	{
	}

	public void Shutdown() 
	{
		// TODO:
		// If we need to explicitly clean up any memory
		// we can call finalize in here as a sanity check.
	}

	/****************************
	 * Accessors
	 *****************************/
	public float getPosX() { return pos.getX(); }
	public float getPosY() { return pos.getY(); }
	public float getVelX() { return vel.getX(); }
	public float getVelY() { return vel.getY(); }
	public float[] getVerts() { return vertices; }
	public FloatBuffer getVB() { return vertexBuffer; }
	public ShortBuffer getIB(){ return indexBuffer; } 
	public FloatBuffer getTexBuffer() { return mTextureBuffer; }
	public int getNumVerts() { return vertices.length; }
	public int getNumIndices() { return indices.length; }
	public int getTextureID() { return mTextureID; }

	/****************************
	 * Mutators
	 *****************************/
	public void setPosX(float _x) { this.pos.setX(_x); }
	public void setPosY(float _y) { this.pos.setY(_y); }
	public void setVelX(float _x) { this.vel.setX(_x); }
	public void setVelY(float _y) { this.vel.setY(_y); }
	public void setTexID(int id) { mTextureID = id; }
}
